using UnityEngine;

public enum Team { Player = 1, Enemy = 2, Neutral = 3 }
public class DataController : MonoBehaviour, IDamageable, IAttacker
{
    [SerializeField] Team team = Team.Neutral;
    public CharacterDatas_SO dataSO;
    public int MaxHealth { get; private set; }
    public int CurrentHealth { get; private set; }
    public int BaseAttack = 2; //敌人生命值
    public void SetTeam(Team t) => team = t;
    public int TeamId => (int)team;
    public event System.Action<int, int> upDateHealthBar;
    private CharacterAnimation characterAnimation;
    private CharacterBase characterBase;
    [SerializeField] int canTakeHit = 1;   // 1=可受击 0=锁定
    [SerializeField] float iframe = 0.08f;
    float nextTime;
    private PlayAudioComp playAudioComp;
    private PlayerController playerController;
    public GameObject HitVFXObject;
    public GameObject BloodEffectPrefab1;
    void Awake()
    {
        playAudioComp = GetComponent<PlayAudioComp>();
        playerController = GetComponent<PlayerController>()!;
        upDateHealthBar?.Invoke(CurrentHealth, MaxHealth);
        characterBase = GetComponent<CharacterBase>();
        characterAnimation = GetComponent<CharacterAnimation>();
        MaxHealth = dataSO ? dataSO.maxHealth : 3;
        CurrentHealth = MaxHealth;

    }
    void Start()
    {
        upDateHealthBar?.Invoke(CurrentHealth, MaxHealth);

    }

    //击打扣血
    public void TakeDamage(int amount, GameObject from)
    {

        if (playerController != null && playerController.isInvincible)
            return;

        //播放打击音效
        playAudioComp.PlayHitAudio();

        //生成打击特效
        PlayAttackVFX(from);

        //扣血
        CurrentHealth = Mathf.Max(0, CurrentHealth - amount);
        upDateHealthBar?.Invoke(CurrentHealth, MaxHealth);

        // 扣血后，如果还有血量
        if (CurrentHealth > 0)
        {
            characterBase.Hit();
        }
        else
        {
            Die();
        }

        // 触发玩家无敌帧（仅对玩家）
        if (playerController != null)
        {
            playerController.StartInvincibility();
        }
    }

    //生成打击特效
    private void PlayAttackVFX(GameObject from)
    {
        if (BloodEffectPrefab1)
        {
            // HitVFX
            //生成打击特效
            GameObject InstantHit = Instantiate(BloodEffectPrefab1, HitVFXObject.transform.position, from.transform.rotation);

            InstantHit.transform.position = new Vector3(0f, 0f, 0f);
            InstantHit.transform.parent = HitVFXObject.transform;
            InstantHit.gameObject.SetActive(true);
        }
        else
        {
            Debug.Log("找不到打击特效");
        }

    }



    //回血
    public void Heal(int amount)
    {
        CurrentHealth = Mathf.Min(MaxHealth, CurrentHealth + amount);
        upDateHealthBar?.Invoke(CurrentHealth, MaxHealth);
        playerController.AddHealthVFX();
    }

    //设置角色速度属性
    public void SetPlayerSpeedData(ItemDatas_SO itemDatas_SO)
    {
        //加当前速度的百分比
        playerController.moveSpeed += playerController.moveSpeed * (itemDatas_SO.itemData * 0.01f);

    }


    //死亡
    void Die()
    {
        //注销监听事件
        upDateHealthBar = null;
        characterBase.setDieState();
    }

    internal void ResetHitFlag() => canTakeHit = 1;


    // ===== IAttacker =====
    public int AttackPower => dataSO ? dataSO.attack : BaseAttack; // 来自 Attack_SO
    public GameObject OwnerObject => gameObject;

}
